package com.mall.controller;

import com.mall.common.AuthCheck;
import com.mall.common.Result;
import com.mall.dto.CartOperateDTO;
import com.mall.service.CartService;
import com.mall.util.JwtUtil;
import com.mall.util.TokenUtils;
import com.mall.vo.CartVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 购物车控制器
 * 
 * @author Mall Team
 */
@Slf4j
@RestController
@RequestMapping("/cart")
@CrossOrigin
public class CartController {
    
    @Autowired
    private CartService cartService;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private TokenUtils tokenUtils;
    
    /**
     * 添加商品到购物车
     */
    @PostMapping("/add")
    @AuthCheck
    public Result<String> addToCart(@Valid @RequestBody CartOperateDTO.AddToCartDTO addToCartDTO) {
        // TODO: 从token中解析用户ID
        String token = tokenUtils.getTokenFromRequest();
        Long userId = jwtUtil.getUserIdFromToken(token);
        cartService.addToCart(userId, addToCartDTO);
        return Result.success("添加成功");
    }
    
    /**
     * 获取购物车列表
     */

    @GetMapping("/list")
    @AuthCheck
    public Result<CartVO.CartSummaryVO> getCartList() {
        // TODO: 从token中解析用户ID
        String token = tokenUtils.getTokenFromRequest();
        Long userId = jwtUtil.getUserIdFromToken(token);
        CartVO.CartSummaryVO cartSummary = cartService.getCartList(userId);
        return Result.success(cartSummary);
    }
    
    /**
     * 更新购物车商品数量
     */
    @PutMapping("/quantity")
    @AuthCheck
    public Result<String> updateCartQuantity(@Valid @RequestBody CartOperateDTO.UpdateCartDTO updateCartDTO) {
        // TODO: 从token中解析用户ID
        String token = tokenUtils.getTokenFromRequest();
        Long userId = jwtUtil.getUserIdFromToken(token);
        cartService.updateCartQuantity(userId, updateCartDTO);
        return Result.success("更新成功");
    }
    
    /**
     * 更新购物车商品选中状态
     */
    @PutMapping("/checked")
    @AuthCheck
    public Result<String> updateCartChecked(@Valid @RequestBody CartOperateDTO.UpdateCartDTO updateCartDTO) {
        // TODO: 从token中解析用户ID
        String token = tokenUtils.getTokenFromRequest();
        Long userId = jwtUtil.getUserIdFromToken(token);
        cartService.updateCartChecked(userId, updateCartDTO);
        return Result.success("更新成功");
    }
    
    /**
     * 批量选择购物车商品
     */
    @PutMapping("/batchSelect")
    @AuthCheck
    public Result<String> batchSelectCart(@Valid @RequestBody CartOperateDTO.BatchSelectDTO batchSelectDTO) {
        // TODO: 从token中解析用户ID
        String token = tokenUtils.getTokenFromRequest();
        Long userId = jwtUtil.getUserIdFromToken(token);
        cartService.batchSelectCart(userId, batchSelectDTO);
        return Result.success("操作成功");
    }
    
    /**
     * 删除购物车商品
     */
    @DeleteMapping("/remove/{cartId}")
    @AuthCheck
    public Result<String> removeFromCart(@PathVariable Long cartId) {
        // TODO: 从token中解析用户ID
        String token = tokenUtils.getTokenFromRequest();
        Long userId = jwtUtil.getUserIdFromToken(token);
        cartService.removeFromCart(userId, cartId);
        return Result.success("删除成功");
    }
    
    /**
     * 批量删除购物车商品
     */
    @PostMapping("/batchRemove")
    @AuthCheck
    public Result<String> batchRemoveFromCart(@RequestBody List<Long> cartIds) {
        // TODO: 从token中解析用户ID
        String token = tokenUtils.getTokenFromRequest();
        Long userId = jwtUtil.getUserIdFromToken(token);

        cartService.batchRemoveFromCart(userId, cartIds.toArray(Long[]::new));
        return Result.success("删除成功");
    }
    
    /**
     * 清空购物车
     */
    @DeleteMapping("/clear")
    @AuthCheck
    public Result<String> clearCart() {
        // TODO: 从token中解析用户ID
        String token = tokenUtils.getTokenFromRequest();
        Long userId = jwtUtil.getUserIdFromToken(token);
        cartService.clearCart(userId);
        return Result.success("清空成功");
    }
    
    /**
     * 获取购物车商品数量
     */
    @GetMapping("/count")
    @AuthCheck
    public Result<Long> getCartCount() {
        // TODO: 从token中解析用户ID
        String token = tokenUtils.getTokenFromRequest();
        Long userId = jwtUtil.getUserIdFromToken(token);
        Long count = cartService.getCartCount(userId);
        return Result.success(count);
    }
    
    /**
     * 获取选中的购物车商品
     */
    @GetMapping("/checked")
    @com.mall.common.AuthCheck
    public Result<CartVO.CartSummaryVO> getCheckedCartItems() {
        // TODO: 从token中解析用户ID
        Long userId = 1L; // 临时写死，后续通过JWT解析
        CartVO.CartSummaryVO cartSummary = cartService.getCheckedCartItems(userId);
        return Result.success(cartSummary);
    }
}